<template>
    	<!-- 通过 -->
		<el-dialog title="新增会员" :visible.sync="addFormVisible" @close="closeDialog"   center>
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm" label-position="left" inline>
				<el-col :span="24">
					<el-form-item label="会员编号" prop="number" v-if="addForm.number">
						<!-- <el-input v-model="addForm.number" auto-complete="off"></el-input> -->
						<div>{{addForm.number}}</div>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="手机" prop="phone" :disabled="false">
						<el-input v-model="addForm.phone" placeholder="输入手机号"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="姓名" prop="realname" >
						<el-input v-model="addForm.realname" auto-complete="off" :disabled="addForm.is_vip==1" style="width:200px"></el-input>
						
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-col :span="14">
						<el-form-item label="性别" prop="sex">
							<el-radio-group v-model="addForm.sex" :disabled="addForm.is_vip==1">
								<el-radio class="radio" :label="0">保密</el-radio>
								<el-radio class="radio" :label="1">男</el-radio>
								<el-radio class="radio" :label="2">女</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="10">
						<el-form-item label="年龄" prop="age" label-width="80px">
							<el-input v-model="addForm.age" style="width:80px" :disabled="addForm.is_vip==1"></el-input>
						</el-form-item>
					</el-col>
				</el-col>
				<el-col :span="24">
					<el-form-item label="籍贯" prop="origin">
						<el-cascader
							placeholder="选择地址"
							:disabled="addForm.is_vip==1"
							:options="ORIGIN"
							:props="{value:'label'}"
							v-model="addForm.origin"
							class="cascader"
							filterable></el-cascader>
					</el-form-item>
				</el-col>
				<el-col :span=24>
					<el-form-item label="激活状态" prop="status">
						<div v-if="addForm.status=='1'">激活</div>
						<div v-else>未激活</div>
					</el-form-item>
				</el-col>
				<el-col :span=24>
					<el-col :span="12">
						<el-form-item label="是否已缴清费用" prop="status" label-width="120px">
							<el-radio-group v-model="addForm.status" :disabled="addForm.is_vip==1">
								<el-radio class="radio" :label="1">是</el-radio>
								<el-radio class="radio" :label="2">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="已缴费用" prop="received" v-if="addForm.status==2">
							<el-input v-model="addForm.received" :disabled="addForm.is_vip==1"></el-input>
						</el-form-item>
					</el-col>
				</el-col>
				
				<el-col :span=24>
					<el-col :span="12">
						<el-form-item label="会员级别" prop="level" width="100px">
							<el-select v-model="addForm.level" placeholder="请选择" @change="levelChange" :disabled="addForm.is_vip==1">
								<el-option
									v-for="item in levelOptions"
									:key="item.code"
									:label="item.name"
									:value="item.code"
									>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="缴费方式" prop="payMode" label-width="80px">
							<el-select v-model="addForm.payMode" placeholder="请选择" :disabled="payModeFlag || addForm.is_vip==1" :allow-create="true">
								<el-option
									v-for="item in payModeOptions"
									:key="item.value"
									:label="item.label"
									:value="item.value"
									:disabled="item.disabled"
									>
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-col>
				
				<el-col :span="24" style="display:flex;justify-content:space-around">
					<el-col :span="12">
						<el-form-item label="入会时间" prop="admissionTime">
							<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="date" placeholder="选择日期" v-model="addForm.admissionTime" :disabled="addForm.is_vip==1"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="会员到期时间" prop="expires" label-width="100px">
						<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="date" placeholder="选择日期" v-model="addForm.expires" :disabled="addForm.is_vip==1"></el-date-picker>
					</el-form-item>
					</el-col>
				</el-col>
				
				<div v-for="(item,index) of addForm.companyList" :key="index">
                    <el-form-item label="企业名称" prop="unit">
					    <el-input v-model="addForm.unit" :disabled="addForm.is_vip==1"></el-input>
                    </el-form-item>
                    <el-form-item label="企业地址" prop="address">
                        
                        <el-cascader
                            placeholder="选择地址"
                            :options="ORIGIN"
                            :disabled="addForm.is_vip==1"
                            :props="{value:'label'}"
                            v-model="addForm.address"
                            class="cascader"
                            ref="origin"
                            filterable></el-cascader>
                            <el-input type="textarea" :disabled="addForm.is_vip==1" v-model="addForm.addressdetail" placeholder="填写详细地址" class="address-detail"></el-input>
                    </el-form-item>
                </div>
                <el-col :span="24">
                    <el-button type="primary" @click="addCompany">添加企业</el-button>
                </el-col>
				<el-col :span="24">
					<el-form-item label="会费标准" prop="cost">
						<!-- <el-select v-model="addForm.cost" placeholder="请选择" disabled>
							<el-option
								v-for="item in costOptions"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>
						</el-select> -->
						<div>{{addForm.cost}}</div>
					</el-form-item>
				</el-col>
				
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<!-- <el-button @click.native="addFormVisible = false">取消</el-button> -->
				<el-button type="primary" @click="submitForm('addForm')" :disabled="addForm.is_vip==1">提交</el-button>
			</div>
		</el-dialog>
		<!-- 通过 end -->
</template>
<script>
import ORIGIN from '../../assets/json/origin.json';
export default {
    data(){
        return{
            addFormVisible:false,//弹窗的显示状态
            id:'',//数据id
            uid:'',//普通用户id
            ORIGIN,//省市区
            payModeFlag:true,//能否选择缴费方式
            addFormRules: {
				name: [
					{ required: true, message: '请输入姓名', trigger: 'blur' }
				],
				phone: [
					{ required: true, message: '请输入手机号', trigger: 'blur' }
				],
				// sex: [
				// 	{ required: true, message: '请选择性别', trigger: 'blur' }
				// ],
				age: [
					{ required: true, message: '请输入年龄', trigger: 'blur' }
				],
				level: [
					{ required: true, message: '请输入会员等级', trigger: 'blur' }
				],
				origin: [
					{ required: true, message: '请输入籍贯', trigger: 'blur' }
				],
				admissionTime: [
					{ required: true, message: '请输入入会时间', trigger: 'blur' }
				],
				unit: [
					{ required: true, message: '请输入单位名称', trigger: 'blur' }
				],
				address: [
					{ required: true, message: '请输入单位地址', trigger: 'blur' }
				],
				// expires: [
				// 	{ required: true, message: '请输入到期时间', trigger: 'blur' }
				// ]
            },
            addForm: {
                noPrint:'',//表单不能进行标记
                is_vip:'',//是否已经为vip，1只能编辑不能新增
				number:'',//会员编号
                realname: '',
                status:'',//激活状态
				phone:'',
				sex: '',
				age: '',
				payMode:'',//缴费方式
				level: '',
				origin:'',//籍贯
				admissionTime:'',//入会时间
				unit:'',
				address: '',
				addressdetail:'',//公司详细地址
				cost:'',
				expires:'',//到期时间
			},
			//会员级别
			levelOptions: [],
			//会费标准
			costOptions: [
				{
					value: '25万/届',
					label: '25万/届'
				},
				{
					value: '15万/届',
					label: '15万/届'
				},
				{
					value: '10万/届',
					label: '10万/届'
				},
				{
					value: '5万/届',
					label: '5万/届'
				},
				{
					value: '1.5万/届',
					label: '1.5万/届'
				},
				{
					value: '1千/年',
					label: '1千/年'
				}
			],
			//会员缴费方式
			payModeOptions:[
				{label:'免费',value:0,disabled:false},
				{label:'一次性',value:1,disabled:false},
				{label:'分期',value:2,disabled:false}
            ],
        }
    },
    watch:{
		addForm:{
			handler:function(nval){
                
				if(nval.level){
					this.payModeFlag=false;//先选中会员等级才能选缴费方式
				}else{
					this.payModeFlag=true;
                }
			},
			deep:true
        },
    },
    methods: {
         handleClose(){
      //编辑弹窗关闭前的回调

	},
	async showAddForm(id,uid){
		//显示通过会员的信息表单
		this.id=id;
		this.uid=uid;
		const data=await this.$api.getUserVipApplyDetail({
                id,
                uid,
			});
            if(data.data.level=='success'){
                let memberDetail=data.data.data.user_vip_apply_info;
                this.addForm.realname=memberDetail.realname;
                this.addForm.phone=memberDetail.phone;
                this.addForm.sex=+ memberDetail.sex;
                this.addForm.age=memberDetail.age;
                this.addForm.payMode=memberDetail.pay_mode;
                this.addForm.level=memberDetail.vip_code;
                this.addForm.origin=[memberDetail.home_province,memberDetail.home_city]
                this.addForm.admissionTime=memberDetail.start_time;
                this.addForm.unit=memberDetail.company_name;
                this.addForm.address=[memberDetail.province,memberDetail.city,memberDetail.area];
				this.addForm.addressdetail=memberDetail.company_address;
				this.addFormVisible=true
				this.levelChange(this.addForm.level);//更新缴费方式的可选项
            }else{
				this.$message({
					message: data.data.message,
					type: 'error'
				});
			}
    },
    levelChange(e){
               //选中会员等级，更改相应缴费方式
            this.addForm.payMode='';//重置缴费方式
            this.payModeOptions=[
                {label:'一次性',value:'1',disabled:false},
                {label:'分期',value:'2',disabled:false}
            ];
            const rule=this.levelOptions.find(val=>{
                return val.code==e
            })
            const list=rule.pay_rule_list;
            const listmatch=list.map(item=>{
                return item.type
            })
            this.payModeOptions.forEach((val,index,arr)=>{
                if(!listmatch.includes(val.value)){
                    let item=val;
                    item.disabled=true;
                    this.$set(this.payModeOptions,index,item)
                }
            })
          },
	 agreeForm(formName) {
            //提交表单验证
             
            this.$refs[formName].validate((valid) => {
                // if(true){
            if (valid) {
                this.getUserVipApplyInsert(this.id,this.uid);
            } else {
                console.log('error submit!!');
                return false;
            }
            });
		  },
	async  getUserVipApplyInsert(id,uid){
		//通过会员申请
		const msg=this.addForm;
		const thsAreaCode = this.$refs['origin'].getCheckedNodes() ;
        const code=thsAreaCode[0].data.value ||'';//获取地区编码
		const req=await this.$api.getUserVipApplyInsert({
			realname:msg.realname,
			phone:msg.phone,
			age:msg.age,
			companyName:msg.unit,
			companyAddress:msg.addressdetail,
			province:msg.address[0],
			city:msg.address[1],
			area:msg.address[2],
			vipCode:msg.level,//会员等级
			payMode:msg.payMode,//1全款 2分期
			areaCode:code,
			sex:msg.sex,
			homeProvince:msg.origin[0],
			homeCity:msg.origin[1],
			startTime:msg.admissionTime,
			id:id,
			uid:uid
		})
            if(req.data.level=='success'){
                this.$message({
                    message: req.data.message,
                    type: 'success'
                    });
                    this.$emit('updateList')
                this.addFormVisible=false;
            }else{
                this.$message({
                message: req.data.message,
                type: 'error'
                });
            }
        },
        async getUserVipApplyRefuse(id,uid){
		//拒绝会员申请
		const judge= this.$confirm('拒绝该用户入会申请?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(async() => {
					const res=await this.$api.getUserVipApplyRefuse({
							id,uid
						})
					if(res.data.level=='success'){
						
						this.$message({
							type: 'success',
							message: res.data.message
						});
					}else{
                        
                        console.log('assdf')
						this.$message({
							type: 'error',
							message: res.data.message
						});
					}
					
				}).catch(() => {
                    this.$emit('updateList');
			});
		
        },
        closeDialog(){
            //重置数据
            this.id='';
            this.vipId=''
            this.addForm= {
                is_vip:'',
				number:'',//会员编号
                realname: '',
                status:'',//激活状态
				phone:'',
				sex: '',
				age: '',
				payMode:'',//缴费方式
				level: '',
				origin:'',//籍贯
				admissionTime:'',//入会时间
				unit:'',
				address: '',
				addressdetail:'',//公司详细地址
				cost:'',
				expires:'',//到期时间
			};
        },
    },
    
}
</script>
